package com.jiyun.play.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.domain.PlayersApplication;
import com.jiyun.play.mapper.PlayersApplicationMapper;
import com.jiyun.play.service.IPlayersApplicationService;
import com.jiyun.play.service.impl.PlayersApplicationServiceImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spyx.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sy
 * @since 2024-12-19
 */
@Service
public class PlayersApplicationServiceImpl extends ServiceImpl<PlayersApplicationMapper, PlayersApplication> implements IPlayersApplicationService {

    private final PlayersApplicationMapper playersApplicationMapper;

    public PlayersApplicationServiceImpl(PlayersApplicationMapper playersApplicationMapper) {
        this.playersApplicationMapper = playersApplicationMapper;
    }

    @Override
    public Result applyfor(PlayersApplication playersApplication) {
        LocalDateTime now = LocalDateTime.now();
        playersApplication.setCreatedAt(now);

        Map<String, String> validationMessages = new HashMap<>();
        validationMessages.put("gameNickname", "请输入正确的游戏昵称");
        validationMessages.put("gameTitle", "请输入正确的游戏名称");
        validationMessages.put("position", "请输入擅长的位置");
        validationMessages.put("characterRole", "请输入常玩的角色");
        validationMessages.put("contactPhone", "请输入联系电话");
        validationMessages.put("skillImage", "游戏截图上传失败");

        for (Map.Entry<String, String> entry : validationMessages.entrySet()) {
            String field = entry.getKey();
            String message = entry.getValue();
            if (playersApplication.getFieldValue(field).isEmpty()) {
                return Result.fail(message);
            }
        }

        if (playersApplication.getPricePerHour() == null || playersApplication.getPricePerHour().compareTo(BigDecimal.ZERO) == 0) {
            return Result.fail("请输入合法的金额");
        }

        if (playersApplication.getDescription().isEmpty()) {
            playersApplication.setDescription("这个人很懒，什么都没有写~");
        } else {
            playersApplication.setStatus(0);
            try {
                playersApplicationMapper.insert(playersApplication);
            } catch (Exception e) {
                // 记录日志
                e.printStackTrace();
                return Result.fail("提交失败，请稍后再试");
            }
        }
        return Result.ok("提交成功");
    }


}
